namespace WorldRacing2.Scenario.WR2TerrainLoader.QadFile
{
    public class Ground
    {
        #region Private Fields

        private short dirtiness;
        private short frontGrip;
        private ObjectName groundName;
        private short noCollision;
        private EnumNoise noise;
        private short rearGrip;
        private EnumSkidmarks skidmarks;
        private short stickiness;
        private ObjectName temp;
        private byte[] unknown = new byte[64];
        private byte[] unknown2 = new byte[14];

        #endregion Private Fields

        #region Public Constructors

        public Ground(string name, short frontGrip, short rearGrip, short stickiness, short dirtyness, EnumSkidmarks skidmarks, EnumNoise noise, bool noCollision)
            : this(name, frontGrip, rearGrip, skidmarks, noise)
        {
            this.NoCollision = noCollision;
            this.stickiness = stickiness;
            this.dirtiness = dirtyness;
        }

        public Ground(string name, short frontGrip, short rearGrip, EnumSkidmarks skidmarks, EnumNoise noise)
        {
            this.dirtiness = 0;
            this.frontGrip = frontGrip;
            this.groundName = new ObjectName(name);
            this.noCollision = 0;
            this.noise = noise;
            this.rearGrip = rearGrip;
            this.skidmarks = skidmarks;
            this.stickiness = 0;
            this.temp = new ObjectName("tempName");
        }

        public Ground()
        {
            this.temp = new ObjectName("temp");
        }

        #endregion Public Constructors

        #region Public Properties

        public short Dirtiness
        {
            get { return dirtiness; }
            set { dirtiness = value; }
        }

        public short FrontGrip
        {
            get { return frontGrip; }
            set { frontGrip = value; }
        }

        public string Name
        {
            get { return this.groundName.ToString(); }
            set { this.groundName = new ObjectName(value); }
        }

        public bool NoCollision
        {
            get { return (noCollision > 0) ? true : false; }
            set { noCollision = (short)((value) ? 1 : 0); }
        }

        public EnumNoise Noise
        {
            get { return noise; }
            set { noise = value; }
        }

        public short RearGrip
        {
            get { return rearGrip; }
            set { rearGrip = value; }
        }

        public EnumSkidmarks Skidmarks
        {
            get { return skidmarks; }
            set { skidmarks = value; }
        }

        public short Stickiness
        {
            get { return stickiness; }
            set { stickiness = value; }
        }

        #endregion Public Properties

        #region Public Methods

        public static Ground FromStream(System.IO.BinaryReader br)
        {
            Ground ground = new Ground();

            ground.groundName = ObjectName.FromStream(br);
            ground.temp = ObjectName.FromStream(br); //Only to add more 32 Bytes;
            ground.dirtiness = br.ReadInt16();
            ground.frontGrip = br.ReadInt16();
            ground.rearGrip = br.ReadInt16();
            ground.stickiness = br.ReadInt16();
            ground.noise = (EnumNoise)br.ReadInt16();
            ground.skidmarks = (EnumSkidmarks)br.ReadInt16();
            ground.unknown = br.ReadBytes(64);
            ground.noCollision = br.ReadInt16();
            ground.unknown2 = br.ReadBytes(14);

            return ground;
        }

        public void ToStream(System.IO.BinaryWriter bw)
        {
            this.groundName.Save(bw);
            this.temp.Save(bw);
            bw.Write(this.dirtiness);
            bw.Write(this.frontGrip);
            bw.Write(this.rearGrip);
            bw.Write(this.stickiness);
            bw.Write((short)this.noise);
            bw.Write((short)this.skidmarks);
            bw.Write(this.unknown);
            bw.Write(this.noCollision);
            bw.Write(this.unknown2);
        }

        public override string ToString()
        {
            return this.Name;
        }

        #endregion Public Methods
    }
}